package com.liziba.demo.slice;

import com.liziba.demo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

public class MainAbilitySlice extends AbilitySlice {

    /** 提取Text组件为成员变量，后续方法需要使用 */
    Text text = null;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // 1、找到组件
        // button - 按钮组件
        Button button = (Button) this.findComponentById(ResourceTable.Id_button);
        // 文本组件
        text = (Text) this.findComponentById(ResourceTable.Id_text);

        // 2、绑定双击事件（给需要点击的组件添加双击事件）
        button.setDoubleClickedListener(this::onDoubleClick);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    public void onDoubleClick(Component component) {

        // component表示的是被双击的组件对象
        int id = component.getId();
        if (ResourceTable.Id_button == id) {
            text.setText("大家好，我是李子捌！");
        }
    }
}
